#!/usr/local/bin/python
# -*- coding: utf-8 -*-

from datetime import datetime, date

from sqlalchemy import Column, Integer, String, Text, Float, Date, and_, text, Boolean
from sqlalchemy.ext.declarative import declarative_base

import init
import toolkit
from init import session
from initLogger import logger

# engine = create_engine("mysql://user:password@hostname/dbname?charset=uft8")


Base = declarative_base()


class BillParseRegulationAlchemy(Base):
    '''
    类功能：用于存储账单的解析规则

    '''
    __tablename__ = "bill_parse_regulation"

    id = Column(Integer, primary_key=True, autoincrement=True)

    bill_type = Column(Text, comment="账单的类型")#默认为解析支付宝账单，后续也可以支持其他账单的解析。

    summary = Column(Text, comment="记账凭证名称")
    property_increame_name = Column(Text, comment="资产增加路径")
    property_decrease_name = Column(Text, comment="资产降低路径")
    ownership_increase_name = Column(Text, comment="权属增加路径")
    ownership_decrease_name = Column(Text, comment="权属降低路径")

    regulation = Column(Text, comment="具体的匹配规则")
    priority = Column(Integer, comment="解析规则的优先级")  #分为5级，从1到5。1级最低，5级最高
    is_merge = Column(Boolean, comment="多条账单是否需要合并为同一条记账凭证")
    is_valid = Column(Boolean, comment="标记该规则匹配的账单是否是一条有效的账单")

    def __init__(self,
                 billType="",
                 summary="",
                 propertyIncreaseName="",
                 propertyDecreaseName="",
                 ownershipIncreaseName="",
                 ownershipDecreaseName="",
                 regulation="",
                 priority=1,
                 isMerge=True,
                 isValid=True
                 ):
        '''
        函数功能：bill_parse_regulation数据库表初始化函数
        :param billType:
        :param summary:
        :param propertyIncreaseName:
        :param propertyDecreaseName:
        :param ownershipIncreaseName:
        :param ownershipDecreaseName:
        :param regulation:
        :param priority:
        :param isMerge:
        :param isValid:
        '''

        self.bill_type = billType
        self.summary = summary
        self.property_increame_name = propertyIncreaseName
        self.property_decrease_name = propertyDecreaseName
        self.ownership_increase_name = ownershipIncreaseName
        self.ownership_decrease_name = ownershipDecreaseName
        self.regulation = regulation
        self.priority = priority
        self.is_merge = isMerge
        self.is_valid = isValid

    def AddRegulationItem(self):
        '''
        函数功能：添加一条规则
        :return:
        '''

        session.add(self)
        session.commit()

    def DelRegulationItem(self, id):
        '''
        函数功能；删除一条规则
        :return:
        '''
        deleteRegulation = session.query(BillParseRegulationAlchemy).filter(BillParseRegulationAlchemy.id == id).first()
        if deleteRegulation:
            session.delete(deleteRegulation)
            session.commit()
            return True
        else:
            return False

    def SearchRegulationByBillType(self, billType):
        '''
        函数功能；根据账单类型查询账单解析规则
        :param billType:
        :return:
        '''

        billRegulation = session.query(BillParseRegulationAlchemy).filter(BillParseRegulationAlchemy.bill_type==billType)  \
                                                           .order_by(BillParseRegulationAlchemy.priority.desc())  \
                                                           .all()
        return billRegulation

def main():
    testAddDataItem()
    print("hello world!")


def testAddDataItem():
    billParseRegulationAlchemy = BillParseRegulationAlchemy( billType="alipay_bill",
                                                             summary="记账凭证摘要",
                                                             propertyIncreaseName = "propertyIncreaseName",
                                                             propertyDecreaseName = "propertyDecreaseName",
                                                             ownershipIncreaseName="ownershipIncreaseName",
                                                             ownershipDecreaseName="ownershipDecreaseName",
                                                             regulation="aaaa",
                                                             priority=1,
                                                             isMerge=True,
                                                             isValid=True)
    billParseRegulationAlchemy.AddRegulationItem()


BillParseRegulationAlchemy.metadata.create_all(init.engine)
if __name__== "__main__":
    main()